<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\models\KeyMap;
use app\widgets\LinkPager;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;
use dosamigos\datepicker\DatePicker;
/**
 * @var $this yii\web\View
 * @var $rechargeList app\models\UserCardRecharge[]
 * @var $pagination yii\data\Pagination
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '卡充值列表';
$this->params['breadcrumbs'][] = '财务管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
<div class="form-group">
    <label for="search_agentname" class="sr-only">代理</label>
    <?php echo Html::textInput('search_agentname', Yii::$app->request->get('search_agentname'), ['id' => 'search_agentname', 'class' => 'form-control', 'placeholder' => '代理', 'style' => 'width: 120px;']);?>
</div>
<div class="form-group">
    <?php echo Html::checkbox('search_sub_agent', Yii::$app->request->get('search_sub_agent') == 1, ['id' => 'search_sub_agent', 'value' => 1]);?>
    <label for="search_sub_agent">包含下级</label>
</div>
<div class="form-group">
    <label for="search_trade_type" class="sr-only">支付方式</label>
    <?php echo Html::dropDownList('search_trade_type', Yii::$app->request->get('search_trade_type'), KeyMap::getValues('user_card_recharge_trade_type'), ['id' => 'search_trade_type', 'class' => 'form-control', 'prompt' => '支付方式']);?>
</div>
<div class="form-group">
    <label for="search_recharge_type" class="sr-only">充值状态</label>
    <?php echo Html::dropDownList('search_recharge_type', Yii::$app->request->get('search_recharge_type'), KeyMap::getValues('recharge_type'), ['id' => 'search_recharge_type', 'class' => 'form-control', 'prompt' => '充值状态']);?>
</div>
<div class="form-group">
    <label for="search_status" class="sr-only">来源</label>
    <?php echo Html::dropDownList('search_source', Yii::$app->request->get('search_source'), KeyMap::getValues('source'), ['id' => 'search_source', 'class' => 'form-control', 'prompt' => '来源']);?>
</div>
<div class="form-group">
    <label for="search_iccid" class="sr-only">ICCID</label>
    <?php echo Html::textInput('search_iccid', Yii::$app->request->get('search_iccid'), ['id'=>'search_iccid', 'class'=>'form-control', 'placeholder'=>'ICCID', 'style' => 'min-width:180px;']);?>
</div>
<div class="form-group">
    <label for="search_trade_no" class="sr-only">交易单号</label>
    <?php echo Html::textInput('search_trade_no', Yii::$app->request->get('search_trade_no'), ['id'=>'search_trade_no', 'class'=>'form-control', 'placeholder'=>'交易单号', 'style' => 'min-width:170px;']);?>
</div>
<div class="form-group">
    <label for="time">起始时间</label>
    <?php echo DatePicker::widget([ 
        'name' => 'search_start_time', 
        'options' => ['placeholder' => '起始时间'], 
        'attribute' => 'time',
        'template' => '{addon}{input}',
        'value' => $search_start_time,
        'clientOptions' => [ 
            'autoclose' => true, 
            'format' => 'yyyy-mm-dd',
            'todayHighlight' => true, 
        ] 
    ]); ?>
</div>
<div class="form-group">
    <label for="time">结束时间</label>
    <?php echo DatePicker::widget([ 
        'name' => 'search_end_time', 
        'options' => ['placeholder' => '结束时间'], 
        'attribute' => 'time',
        'template' => '{addon}{input}',
        'value' => $search_end_time,
        'clientOptions' => [ 
            'autoclose' => true, 
            'format' => 'yyyy-mm-dd',
            'todayHighlight' => true, 
        ] 
    ]); ?>
</div>
<div class="form-group">
    <button class="btn btn-primary btn-sm">搜索</button>
    <a href="<?php echo Url::current(['export'=>'excel']);?>">导出Excel</a>
</div>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl">编号</span>
            </label>
        </th>
        <th class="center">代理商</th>
        <th class="center">ICCID/MSISDN/IMSI</th>
        <th class="center">套餐</th>
        <th class="center">时长</th>
        <th class="center">成本价</th>
        <th class="center">销售价</th>
        <th class="center">支付方式</th>
        <th class="center">充值状态</th>
        <th class="center">交易单号</th>
        <th class="center">来源</th>
        <th class="center">创建时间</th>
        <th class="center">详情/退款</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($rechargeList as $recharge) {?>
        <tr>
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $recharge->id;?>" /><span class="lbl"><?php echo $recharge->id;?></span></label></td>
            <td class="center"><?php echo empty($recharge->agent_id) ? '----' : Html::a(Html::encode($recharge->agent->username), ['/admin/user/agent-view', 'id' => $recharge->agent_id]);?></td>
            <td class="center"><?php echo empty($recharge->card->iccid) ? '----' : Html::encode($recharge->card->iccid);
                echo '<br />';
                echo empty($recharge->card->msisdn) ? '----' : Html::encode($recharge->card->msisdn);
                echo '<br />';
                echo empty($recharge->card->imsi) ? '----' : Html::encode($recharge->card->imsi);?></td>
            <td class="center"><?php echo Html::encode($recharge->package->name);?></td>
            
            <td class="center"><?php echo $recharge->term_length, KeyMap::getValue('card_term_length_unit', $recharge->term_length_unit);?></td>
            <td class="center"><?php
                if(empty($recharge->packages->cost_price)){
                    echo sprintf("%.2f",$recharge->package->price);
                }else{
                    echo  sprintf("%.2f",$recharge->packages->cost_price);
                }
            ?></td>
            <td class="center"><?php
                if(empty($recharge->packages->sale_price)){
                    echo sprintf("%.2f",$recharge->package->price);
                }else{
                    echo sprintf("%.2f",$recharge->packages->sale_price);
                }
            ?></td>
            <td class="center"><?php echo KeyMap::getValue('user_card_recharge_trade_type', $recharge->trade_type);?></td>
            <td class="center">
                <?php echo empty($recharge->recharge_type)?'----':Html::encode(KeyMap::getValue('recharge_type',$recharge->recharge_type));?>
            </td>
            <td class="center"><?php echo Html::encode($recharge->trade_no);?></td>
            <td class="center"><?php echo KeyMap::getValue('source',$recharge->source);?></td>
            <td class="center"><?php echo Yii::$app->formatter->asDatetime($recharge->create_time);?></td>
            <td class="center"><?php echo ManagerTableOp::widget(['items' => [
                    ['icon' => 'fa fa-info-circle','onclick' => 'view('.$recharge->id.')','btn_class' => 'btn btn-xs btn-default','tip' => '卡充值详情'],
                    !($recharge->recharge_type != 5) ?:['icon' => 'fa fa-jpy', 'onclick' => 'setRefund(' . $recharge->id . ',' . $recharge->trade_type . ','.$recharge->source.')', 'btn_class' => 'btn btn-xs btn-warning', 'tip' => '退款'],
                    // ['icon' => 'fa fa-trash', 'onclick' => 'deletelog('.$recharge->id.')', 'btn_class' => 'btn btn-xs btn-danger','tip' => '删除', 'color'=>'red']
                ]]);?>
            </td>
        </tr>
    <?php }?>
    </tbody>
</table>
<?php echo LinkPager::widget(['pagination' => $pagination]);?>
<script>
    /**
     * 设置已支付
     * @param id 充值编号
     */
    function setPaid(id) {
        $.getJSON('<?php echo Url::to(['/admin/finance/set-recharge-paid']);?>', {id: id}, function (json) {
            if (callback(json)) {
                layer.msg('设置成功。', function () {window.location.reload();});
            }
        });
    }
    function deletelog(id) {
        if (!confirm('确定要删除吗？')) {
            return false;
        }
        $.getJSON('<?php echo Url::to(['/admin/finance/delete-recharge']);?>', {'id':id}, function(json) {
            if (callback(json)) {
                layer.msg('删除成功', function () {window.location.reload();});
            }
        });
    }
    /**
    * 弹出卡充值详情    
     */
    function view(id){
        url = "<?php echo Url::to(['/admin/finance/recharge-view']);?>?id="+id;
        layer.open({
        type: 2, 
        closeBtn: 2,
        title:'卡充值详情',
        offset: 'auto',
        area: ['750px', '800px'],
        shade: 0.5,
        shadeClose:true,
        content: url 
        }); 
    }
    function setRefund(id,trade_type,source){
        layer.prompt({
        formType: 2, 
        value: '',
        placeholder:"请填写退款原因",
        title: '填写退款原因(必填)',
        offset: 'auto',
        shade:0.5,
        shadeClose:true,
        area: ['350px', '120px'],
        }, function(value, index, elem){
            if($('#money').val()===""){
                layer.tips("请填写退款金额",$('#money'));
                return;
            }
            var money=$('#money').val();
            layer.confirm('确定要提交退款吗？', {
                btn: ['确定','取消'] 
            },          
            function(){
                layer.msg('提交退款请求成功', {icon:1,time: 1000});
                $.getJSON('<?php echo Url::to(['/admin/finance/set-recharge-refund']);?>', {id:id,trade_type:trade_type,source,value,money}, function (json) {
                    if (json.code == "200") {
                        layer.msg(json.msg, {icon: 1},function () {window.location.reload();});
                    }else{
                        layer.msg(json.msg,{icon: 2});
                    }
                });
                layer.close(index);
            }
            );
        }); 
        $(".layui-layer-content").append("<br/><input type=\"text\" id= \"money\" class=\"layui-input\" style= \"width:350px\" placeholder=\"填写退款金额\" />");
    }
</script>
